var noneSelectImgSrc = "";
var selectImgSrc = "";
var shopCar;


//全选按钮切换
allSelectImg.onclick = function(){
    this.src = this.src == selectImgSrc?
        noneSelectImgSrc:selectImgSrc;

    var allSelectImgSrc = this.src;

    // jquery 中循环函数 each
    // 这个函数有两个参数，
    // index 表示循环索引
    // ele 表示循环的元素（标签）
    // .ckimg 就是每个商品前面选择图片
    $(".ckimg").each(function(index,ele){
        ele.src = allSelectImgSrc;
    });
    jisuanPrice();
};


//加载数据
window.onload = function() {
    // 显示loading
    $.showPreloader();
    $("#shopCarDiv").hide();
    var loadedImageCount = 0;

    shopCar = JSON.parse(localStorage.getItem("shopCar"));
    if (!shopCar||shopCar.length==0) {
        emptyCar.style.display = "block";
        bottom2.style.display = "none";
        $.hidePreloader();
    } else {
        bottom2.style.display = "block";
        shopCarCount.innerHTML = "购物车("+ shopCar.length+")";
        for (var i = 0; i < shopCar.length; i++) {
            var product = shopCar[i];
            var productDiv = document.createElement("div");
            productDiv.className = "product";
            productDiv.setAttribute("name","product0");

            var selectImg = document.createElement("img");
            selectImg.src = noneSelectImgSrc;
            selectImg.style.height = "20px";
            selectImg.className = "ckimg";
            selectImg.onclick = function(){
                this.src = this.src == selectImgSrc
                    ?noneSelectImgSrc:selectImgSrc;
                jisuanPrice();
            };

            var productImage = document.createElement("img");
            productImage.src = product.pic;
            productImage.style.height = "100px";
            productImage.onload = function(){
                loadedImageCount++;
                if(loadedImageCount==shopCar.length){
                    $.hidePreloader();
                    $("#shopCarDiv").show(300);
                }
            };

            var productInfoDiv = document.createElement("div");


            //标题内容div
            var titleDiv = document.createElement("div");
            titleDiv.className = "titleDiv";
            titleDiv.innerHTML = product.title;

            //价格div
            var priceDiv = document.createElement("div");
            priceDiv.innerHTML = "&yen; " + product.price;

            productInfoDiv.appendChild(titleDiv);
            productInfoDiv.appendChild(priceDiv);

            //input盒子div
            var inputDiv = document.createElement("span");
            inputDiv.className = "inputs";

            //减法按钮
            var minusDiv = document.createElement("input");
            minusDiv.name = "minus";
            minusDiv.type = "button";
            minusDiv.value = "-";
            minusDiv.nid = product.nid;
            minusDiv.onclick = function(){
                minusProductAmount(this,this.nid)
            };
            inputDiv.appendChild(minusDiv);

            //数量
            var amountDiv = document.createElement("input");
            amountDiv.name = "amount";
            amountDiv.type = "button";
            amountDiv.value = product.count;
            inputDiv.appendChild(amountDiv);

            //加法按钮
            var plusDiv = document.createElement("input");
            plusDiv.name = "plus";
            plusDiv.type = "button";
            plusDiv.value = "+";
            plusDiv.nid = product.nid;
            plusDiv.onclick = function(){
                plusProductAmount(this,this.nid)
            };
            inputDiv.appendChild(plusDiv);

            //每件商品增加一个删除按钮
            var deleteBtn = document.createElement("img");
            deleteBtn.src = "images/垃圾桶.png";
            deleteBtn.nid = product.nid;
            deleteBtn.onclick = function(){
                deleteProductFromShopCart(this,this.nid);
            };


            var threeDiv = document.createElement("div");
            threeDiv.className = "inputs";
            threeDiv.appendChild(inputDiv);
            threeDiv.appendChild(deleteBtn);
            productInfoDiv.appendChild(threeDiv);




            productDiv.appendChild(selectImg);
            productDiv.appendChild(productImage);
            productDiv.appendChild(productInfoDiv);

            shopCarDiv.appendChild(productDiv);

        }
    }
};

//删除商品
function deleteProductFromShopCart(deleteBtn,nid){


    for(var i=0;i<shopCar.length;i++){
        // 如果购物车对象中已经存在了这个商品
        if(shopCar[i].nid == nid){
            $.confirm('您确定要移除该宝贝吗?', function () {
                shopCar.splice(i,1);
                console.log(i);
                $(deleteBtn).parents(".product").css("height",
                    $(deleteBtn).parents(".product").height());
                setTimeout(function(){
                    $(deleteBtn).parents(".product").css("opacity",0);
                    $(deleteBtn).parents(".product").css("height",0);
                },1);
                shopCarCount.innerHTML = "购物车("+ shopCar.length+")";
                $.toast("移除成功！");
                if(shopCar.length==0){
                    emptyCar.style.display = "block";
                    bottom2.style.display = "none";
                }
                // 存进本地存储，替换原来的购物车信息
                localStorage.setItem("shopCar",JSON.stringify(shopCar));
            });

            break;
        }
    }




}

//加法
function plusProductAmount(plusDiv,nid) {
    for(var i=0;i<shopCar.length;i++){
        if(shopCar[i].nid == nid){
            shopCar[i].count++;
            plusDiv.previousElementSibling.value++;
            localStorage.setItem("shopCar",JSON.stringify(shopCar));
            jisuanPrice();
        }
    }
}

//减法
function minusProductAmount(plusDiv,nid) {
    for(var i=0;i<shopCar.length;i++){
        if(shopCar[i].nid == nid){
            if(shopCar[i].count>1){
                shopCar[i].count--;
                plusDiv.nextElementSibling.value--;
                localStorage.setItem("shopCar",JSON.stringify(shopCar));
                jisuanPrice();
            }

        }
    }
}

//计算总价

var cont = document.getElementsByClassName("product");
var total = 0;
function jisuanPrice() {
    for(var i=0;i<cont.length;i++){
        if(cont[i].firstElementChild.src == selectImgSrc){
            var jiage = parseFloat(cont[i].lastElementChild.firstElementChild.nextElementSibling.innerHTML.substring(1))*parseInt(cont[i].lastElementChild.lastElementChild.firstElementChild.firstElementChild.nextElementSibling.value);
            total = total + jiage;
        }
    }
    totalPrice.innerHTML = "&yen;"+total.toFixed(2);
}


// 提交订单

tijiaoBtn.onclick = function () {
    if(total != 0){

        generateOrder();
    }
    else{
        $.toast("您还没有选中宝贝偶！");
    }
};

function generateOrder(){
    location = 'http://test.hfjava.com/shop/zfbpay?price='+total+'&subject=dfdf&body=dfdfr';
}